package com.neusoft.bizcore.auth.fixture;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.model.AuthOganization;
import com.neusoft.bizcore.auth.repository.AuthOganizationRepository;
import com.neusoft.bizcore.auth.stress.AuthFixtureEvent;

@Profile("fixture")
@Component
public class InitialDataFixture implements CommandLineRunner {

    @Autowired
    private AuthOganizationRepository authOganizationRepository;
    @Autowired
    ApplicationContext applicationContext;

    @Override
    public void run(final String... args) throws Exception {
        this.init();
        this.applicationContext.publishEvent(new AuthFixtureEvent(this));
    }

    private void init() {

        // 顶级节点
        AuthOganization rootOrg = this.authOganizationRepository.findByCode("root");
        if (null == rootOrg) {
            rootOrg = new AuthOganization();
            rootOrg.setCode("root");
            rootOrg.setName("安徽省农村信用社联合社");
            rootOrg = this.authOganizationRepository.save(rootOrg);
        }

    }

}
